/**
 *  irq.h - Interruption management.
 *
 *  Copyright (C) 2008-2009 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __IRQ_H__
#define __IRQ_H__


#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/list.h"
#include "include/mem.h"
#include "include/core.h"
#include "hw_irq.h"

#define mac_disable_irq() DisableInt()
#define mac_enable_irq() EnableInt()
#endif


/* 'isr_t' describles a ISR of a interruption */
typedef struct isr_desc {
    void(*pisr)(void);  /* The entry of ISR */
    list_t link;        /* All ISR will linked by this member */
    uword_t irq_no;     /* ISR Serial number for a interruption */
}isr_t;


typedef struct isr_head {
    list_t link;
    uword_t nr;
} isr_head_t;


void initirq(void); /* Initialize interruption management */
void isr_install(uword_t IntNum,     /* Serial number of interruption
                                        that vary from architecture. */
                 uword_t IrqNo,       /* ISR Serial number for a interruption */
                 void(*phdl)(void)); /* Pointer to the entry of ISR */

/* 'isrInstall' has the same function as 'isr_install'.
 * The difference is that 'isr_install' executes in a unprotected context.
 */
#define isrInstall(IntNum, IrqNo, phdl) \
do{\
    mac_disable_irq();\
    isr_install(IntNum,IrqNo,phdl);\
    mac_enable_irq();\
}while(0);

void isr_uninstall(uword_t IntNum, uword_t IrqNo);
#define isrUnInstall(IntNum, IrqNo) \
do{\
    mac_disable_irq();\
    isr_uninstall(IntNum,IrqNo);\
    mac_enable_irq();\
}while(0);

void isrHandler(void);  /* Handle all interruptions */

/* isr_after_handle - this rountine should be called after leaving
 *                    'isrHandler', so that some CPU registers can be clear.
 *                    It should be rewrite when port Bycore to another CPU.
 */
void isr_after_handle(uword_t isr_num);


uword_t int_nesting(void);
bool_t int_flag(void);
void int_nesting_sub(void);
void int_flag_clr(void);
void int_flag_set(void);


#endif

